﻿// JavaScript Document
var APP_ITEM_HEIGHT = 38;
var SCROLL_HEIGHT = 4 * APP_ITEM_HEIGHT;

function changeHeaderUI(){
	$(".taskbar").css("width",$(window).width() - $(".logo").width() - $(".user_info_area").width());
}
function showUserInfo(){
	//获取个人信息图标的位置
	var userInfoOffsetLeft = $("#user_info_control").offset().left;
	var userInfoOffsetTop = $("#user_info_control").offset().top;
	//需要展示的html元素
	//var showHTML = "<div class='userInfoPanel'>123</div>";
	//调整panel的位置
	$(".userInfoPanel").css("left",userInfoOffsetLeft + $("#user_info_control").width()/2 - $(".userInfoPanel").width()/2);
	$(".userInfoPanel").css("top",$("#user_info_control").height());
	$(".userInfoPanel").css("display","");
	//绑定点击状态图标事件
	$(".userState").bind("click",function(){
		$(".chooseState").css("display","");
	});
	//绑定选择状态的div
	$(".chooseState div").bind("click",function(){
		$(".userState").attr("src","images/"+$(this).attr("id")+".png");
		$(".chooseState").css("display","none");
	})
}
/**
 * 菜单底部a标签提示和事件
 */
function bindSideBarFooterTips(){
    var tips ;
    $(".footerLink a").unbind();
    $(".footerLink a").bind("mouseover",function(event){
        var event = event ? event : window.event;
        var target = event.srcElement ? event.srcElement : event.target;
        tips = layer.tips($(target).attr("data-title"),this,{
            tips:[1,'#000']
        });
    });
    $(".footerLink a").unbind();
    $(".footerLink a").bind("mouseout",function(){
        layer.close(tips);
    });
    $("#handMenuWord").unbind();
    $("#handMenuWord").bind('click',function(){
        if($("#handMenuWord").hasClass("glyphicon-chevron-left")){
            $("#handMenuWord").attr("data-title","图标+文字模式").removeClass("glyphicon-chevron-left").addClass("glyphicon-chevron-right");
            $("#sideBar").width("50px");
            $(".secondPanel").css({
                left : "50px"
            });
            $(".footerLinkIcon").hide();
            if($("body").hasClass("right-mini")){
                $("body").removeClass().addClass("TOS1 right-mini");
            }else{
                $("body").removeClass().addClass("TOS1");
            }
        }else{
            $("#handMenuWord").attr("data-title","图标模式").removeClass("glyphicon-chevron-right").addClass("glyphicon-chevron-left");
            $("#sideBar").width("200px");
            $(".secondPanel").css({
                left : "200px"
            });
            $(".footerLinkIcon").show();
            if($("body").hasClass("right-mini")){
                $("body").removeClass().addClass("TOS right-mini");
            }else{
                $("body").removeClass().addClass("TOS");
            }
        }

        $("#center .swiper-slide").css({
            width : $("#center").width()-50
        });
        if(mySwiper != undefined){
            mySwiper.resizeFix();  //当你改变swiper 的尺寸而没有改变窗口大小时调用这个方法。
        }
    });
}

$(document).ready(function(){
    var tips ;
	$(".userInfoPanel").css("display","none");
	$(".chooseState").css("display","none");
	changeHeaderUI();
	//绑定右上角个人中心图片的鼠标滑过效果
	$(".user_info_menu").bind("mouseover",function(){
		//图片更换
		$(this).find("img").attr("src",$(this).find("img").attr("src").substring(0,$(this).find("img").attr("src").length-4)+"-s.png");
		$("#user_info_control").bind("mouseover",function(){
			tips = layer.tips("我的云台账号",this,{
				tips:[4,'#000']
			});	
			//显示个人信息
			showUserInfo();
		});

	});
	//绑定右上角个人中心图片的鼠标离开效果
	$(".user_info_menu").bind("mouseout",function(){
		//图片更换
		$(this).find("img").attr("src",$(this).find("img").attr("src").substring(0,$(this).find("img").attr("src").length-6)+".png");
		layer.close(tips);
		$(".userInfoPanel").bind("mouseover",function(){
			$(".userInfoPanel").css("display","");										 
		});
		$(".userInfoPanel").bind("mouseout",function(){
			$(".userInfoPanel").css("display","none");										 
		});
		$(".userInfoPanel").css("display","none");	
		
	});
	//绑定系统菜单鼠标滑过的效果
	$("#system_menu").bind("mouseover",function(){
		tips = layer.tips("菜单",this,{
			tips:[3,'#000']
		});											
	});
	//绑定系统菜单鼠标离开的效果
	$("#system_menu").bind("mouseout",function(){
		layer.close(tips);									
	});
    //绑定通知中心鼠标滑过的效果
    $("#eastbar").bind("mouseover",function(){
        tips = layer.tips("通知中心",this,{
            tips:[3,'#000']
        });
    });
    //绑定通知中心鼠标离开的效果
    $("#eastbar").bind("mouseout",function(){
        layer.close(tips);
    });
    //绑定通知中心点击效果
    $("#eastbar").bind("click",function(){
        if(!$(this).hasClass("active")){
            $(this).addClass("active");

            //显示通知中心
            $("body").addClass("right-mini");
            msg_closeNotice();
        }else{
            $(this).removeClass("active");

            //关闭通知中心
            $("body").removeClass("right-mini");
        }
    });

    //绑定通知中心小菜单切换事件
    bindEastNavPill();

    //创建左侧菜单
    createMenu();

    //菜单底部a标签提示和事件
    bindSideBarFooterTips();

    //显示默认的二级菜单
    $("#tabs-container").tabs(
        "add",{
            id : 'tab-main-page',
            closable : true,
            title : "欢迎页",
            style : "",
            selected : "true",
            colse: 'false',
            content : "",
            url :"/welcome.html"
        }
    );
//    $("#tabs-container").tabs(
//        "add",{
//            id : 15,
//            closable : true,
//            title : "个人桌面",
//            style : "",
//            selected : "true",
//            content : "",
//            url :"page/personal/desktop.html"
//        }
//    );
    $("#tabs-container").css({
        maxWidth : $("#tabs-container").parent().width()-200
    })
});

//窗口变化事件
$(window).resize(function(){
	//process here
	//绑定通知中心小菜单切换事件
    bindEastNavPill();

    //创建左侧菜单
    createMenu();

    //菜单底部a标签提示和事件
    bindSideBarFooterTips();

});

$(function(){
	
	var initBaseTheme = function(color){
		$('.sideBarFooter ul.footerLinks .footerLink a').css('color',color);
		
		$('.header').css('background',color); 
		$('.bootstrap-switch-primary').css('background',color);
		
	};
	
	var changeTheme = function(obj,themes){
		var href = obj.href;
		if(/[ -~]*\/css\/core\/easyui\/[ -~]*\/easyui.css(\?[ -~])*/.test(href)){
			var ck = '/css/core/easyui/';
			var start = href.indexOf(ck);
			var end =  href.indexOf('/easyui.css');
			href = href.substring(0,start + ck.length) + themes + href.substring(end);
			obj.href = href;
		}
	
	};
	
	var changeThemes = function(themes,color){
		initBaseTheme(color);
		//<link rel="stylesheet" href="/css/core/easyui/<%=themePath %>/easyui.css?v=201505241044"/>  动态替换css
		$('link').each(function(e,d){
			changeTheme(d,themes);			
		});

		for(var key=0;key<window.frames.length;key++){
			var frames = window.frames[key];
			try{
				$(frames.document).find('link').each(function(e,d){
					changeTheme(d,themes);	
				});
			}catch(e){alert(e)}
		}
	};
	
	var createThemes = function(){
		var themesCookie = $.cookie('themes_cookie');
		if(!themesCookie || themesCookie == '') themesCookie ='metro-blue';
		var themesUl = $('#themes');
		for(var key in themesContent){
			var backgroundcolor = themesContent[key].color
			var li = $('<li>');
			li.attr('data',key);
			li.attr('background',backgroundcolor);
			li.css({
				background :backgroundcolor
			});
			themesUl.append(li);
			
			if(key ==themesCookie ){
				li.addClass('current-themes');
				initBaseTheme(themesContent[key].color);
			}
			li.bind('click',function(){
				$('#themes .current-themes').removeClass('current-themes');
				$(this).addClass('current-themes');
				var themesData = $(this).attr('data');
				var background = $(this).attr('background');
				$.cookie('themes_cookie', themesData); // 存储
				changeThemes(themesData,background);				
			});
		}
	};
	
	createThemes();
	 
	
	/**调度在线人数**/
	function setOnlineContent (){
		$.post('/onlineStatisticsController/getOnlineNumber',{},function(d){
			$('#online-count').html('在线人数:' + d);
		});
	}
	/**物业物业费用统计**/
	function setPropertyContent (){
		$.post('/propertyFeeStatisticsController/getAmountSum',{},function(d){
			$('#propertyfee-count').html('总收物业费:' + (d/100) + '元');
		});
	}	
	
	setOnlineContent();
	setPropertyContent();
	setInterval(setOnlineContent,60000);
	setInterval(setPropertyContent,600000);
	
});

/**修改用户密码*/
function updatepwd(){
	parent.$.modalDialog({
		title : '修改密码',
		width : 400,
		height : 220,
		href : '/views/system/updatepwd.jsp',
		buttons : [ {
			text : '确认修改',
			handler : function() {
				var f = parent.$.modalDialog.handler.find('#form');
				f.submit();
			}
		} ]
	});
}

/**消息通知*/
(function(){
	var messageFlag = window.messageFlag  = false;
	var methods = {
			openmsg : function(code,url,param,name){
				if(url.lastIndexOf('?') > 0 && url.lastIndexOf('?') +1 > url.length && param){
					url += "&" + param;
				} else if(param){
					url +="?" + param;
				}
				$('#cal_list_test .' + code).remove();
				createTab($.trim(code),name,url);
				closeEastTab();
			},	
			createHtml : function(datas){
		
			},
			/**
			 * 关闭消息
			 */
			closeNotice : function(){
				try{
					 var c = $('#eastbar');
					 if(c.hasClass("blink")){
						 c.removeClass('blink');
					 }
					clearInterval(msgnotice);
					message_voice_content.pause();
				} catch(e){}
			},
			doNotice :function() {
				//检查是否是开启状态				
				 if($("#eastbar").hasClass("active")) return ;
				if(message_voice_content.paused){
					message_voice_content.load();
					message_voice_content.play();
				}
				 clearInterval(msgnotice);
				msgnotice =  setInterval(function(){
					 var c = $('#eastbar');
					 if(c.hasClass("blink")){
						 c.removeClass('blink');
					 } else {
						 c.addClass('blink');
					 }
				 }, 500);
				 
			},
			post : function(){
				$.post('/messageTask/messages',{},function(datas){
					//var me = this;
					
					if(!datas || datas.length  == 0) {
						$('#cal_list_test').html('没有可显示的消息');
						msg_closeNotice();
						return;
					}
					$('#cal_list_test').html('');
					
					for(var i=0;i<datas.length;i++){
						var data = datas[i];
						var $li = $('<li>');
						$li.addClass(data.code);
					    var $a = $(' <a class="common-font" cal_type="calendar" cal_id="42" href="javascript:;" onclick="msg_openmsg(\''+data.code+ '\',\''  + data.url +'\',\'' + data.param +'\',\' ' +data.typeName+'\');">');
				/*	    $a.bind('click',function(){
					    	msg_openmsg(data.code,data.url,data.param,data.typeName);
					    });*/
					    var content = '';
					    if(data.content){
					    	content = data.content;
					    } else {
					    	content = '你有<span style="color:red;">' + data.count + '</span>条 消息需要处理';
					    }
					    $a.html('<span class="cal_content"><strong style="color:#5b99ee;">【'+data.typeName+'】</strong>'+content+'</span>');
					    $li.append($a);
						$('#cal_list_test').append($li);
					}			
					msg_doNotice();
				},'json');
			}
	};
	
	window.msg_openmsg = methods.openmsg;
	window.msg_doNotice = methods.doNotice;
	window.msg_closeNotice = methods.closeNotice;
	window. msgnotice  = null;
	window.message_voice_content = document.getElementById('message_voice');
	
	methods.post();
	
	setInterval(methods.post, 60 * 1000);
	
})();

